/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function loadCommends(page, style, flag, id) {
	if (page == null) {
		page = 1;
	}
	f$("curPage").value = page;
	var ajax = new Ajax();
	ajax.requestURL = "commend.action";
	ajax.setVar("action", "load");
	ajax.setVar("flag", flag);
	ajax.setVar("id", id);
	ajax.setVar("page", page);
	ajax.setVar("pageCount", (f$("pageCount").value == "" ? 20
			: f$("pageCount").value));
	ajax.method = "GET";
	ajax.onLoading = function() {
		f$("commends").innerHTML = "加载数据中...";
	}
	ajax.onCompletion = function() {
		var jsonObj = JSON.parse(ajax.xmlhttp.responseText);
		renderWall(jsonObj, style);
		renderSeg(jsonObj, style, flag, id);
	}

	ajax.runAJAX();
}

function renderWall(obj, style) {
	var msgLen = obj.length;
	f$("commends").innerHTML = "";
	if (msgLen == 1) {
		f$("commends").style.border="0";
		return;
	}
	for ( var i = 1; i < msgLen; i++) {
		f$("commends").innerHTML += "<div style='padding:5px;margin:5px;border:1px solid "
				+ style
				+ "'><div style='height:20px;background-color:#EFF6FF'><span style='float:left'>"
				+ blankRep(2)
				+ "<font style='font-weight:bold'>"
				+ obj[i].userName
				+ "</font></span><span style='float:right'>"
				+ obj[i].gmtCreate
				+ "</span></div>"
				+ "<div>"
				+ obj[i].commentContent + "</div></div>";
	}
}

function renderSeg(obj, style, flag, id) {
	var curPage = f$("curPage").value;
	var totalCount = obj[0].totalCount;
	f$("commendcount").innerHTML = totalCount;
	var pageCount = obj[0].pageCount;
	var seg = Math.ceil(totalCount / pageCount);
	f$("page_seg").innerHTML = "<div>";
	for ( var i = 1; i <= seg; i++) {
		var _out = "this.style.backgroundColor='white';this.style.color='navy';";
		var _over = "this.style.backgroundColor='" + style
				+ "';this.style.color='white';";
		if (curPage == i) {
			_out = _over;
			_over = "";
		}
		f$("page_seg").innerHTML += "<div style='width:10px;height:10px;float:left;' >&nbsp;</div> "
				+ "<div onmouseout=\""
				+ _out
				+ "\""
				+ " onmouseover=\""
				+ _over
				+ "this.style.cursor='pointer'\"   onclick=\"loadCommends('"
				+ i
				+ "','"
				+ style
				+ "','"
				+ flag
				+ "','"
				+ id
				+ "')\""
				+ " style='"
				+ ((curPage == i) ? "color:white;background-color:" + style
						+ ";" : "")
				+ "line-height:20px;text-align:center;float:left;width:20px;height:20px;border:1px solid "
				+ style + ";'>" + (i) + "</div>";
	}
	f$("page_seg").innerHTML += "</div>";
}

function addCommends(obj, style, flag, id) {
	if (f$("commend").value == "") {
		alert("评论内容不能为空!");
		return false;
	}
	var ajax = new Ajax();
	ajax.requestURL = "commend.action";
	obj.style.disabled = true;
	ajax.setVar("action", "add");
	ajax.setVar("flag", flag);
	ajax.setVar("id", id);
	ajax.setVar("name", f$("name").value);
	ajax.setVar("commend", f$("commend").value);
	ajax.method = "GET";
	ajax.onCompletion = function() {
		f$("name").value = "";
		f$("commend").value = "";
		loadCommends(null, style, flag, id);
		obj.style.disabled = false;
	}
	ajax.runAJAX();
}